﻿namespace ChinaUnicom.Tasks
{
    using System;
    using System.Linq;
    using ChinaUnicom.Models;
    using Avatar.Schedule;
    using Avatar;
    using System.Net;

    public class FeeHistoryTask : ITask
    {
        public bool NotSupportedHistory { get; private set; }

        const string FeeName = "消费合计";

        public FeeHistory History { get; private set; }

        public FeeHistoryTask(int year, int month)
        {
            History = new FeeHistory();
            History.UpdateOccurred(year, month);
        }

        public void Start()
        {
            if (Started != null)
            {
                Started(this, EventArgs.Empty);
            }

            Factory.Operation.RequestFeeHistory(History.Occurred.Year, History.Occurred.Month, x =>
            {
                try
                {
                    if (x.Status == WebExceptionStatus.Success)
                    {
                        /*
                            月固定费:96.00元 
                            增值业务费:7.00元 
                            上网费:1.08元 
                            调增减项:-6.00元 
                            消费合计:98.08元 
                            抵扣合计:98.08元 
                            实际应缴合计:0.00元 */

                        History.Items = Factory.Parser.Parse(x.Result, ItemType.FeeHistory);

                        var fee = History.Items.SingleOrDefault(f => f.Name.IgnoreCaseEquals(FeeName));
                        if (fee != null)
                        {
                            History.Fee = fee.Value;
                        }
                        else
                        {
                            // 没有 消费合计 一项，表示没有消费历史记录 了
                            NotSupportedHistory = true;
                            if (ApplicationSettings.Settings.PrimelyFeeHistoryDate < History.Occurred)
                            {
                                ApplicationSettings.Settings.PrimelyFeeHistoryDate = History.Occurred;
                                if (Context.Current.FeeHistories.Count > 0)
                                {
                                    var min = Context.Current.FeeHistories.Min(m => m.Occurred);
                                    if (min != null)
                                    {
                                        ApplicationSettings.Settings.PrimelyFeeHistoryDate = min < History.Occurred ? min : History.Occurred;
                                    }
                                }
                            }
                        }

                        Context.Current.Account.LastUpdated = DateTime.Now; 

                        if (Completed != null)
                        {
                            Completed(this, EventArgs.Empty);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (Failed != null)
                    {
                        Failed(this, new FailedEventArgs(this, ex));
                    }
                }
            });
        }

        public int Delay { get { return 50; } }

        public bool IsUnique { get { return false; } }

        public event EventHandler Completed;

        public event EventHandler Started;

        public event EventHandler<FailedEventArgs> Failed;
    }
}
